package com.heifeng.cloudshelf.mvp.search.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.base.MyApp;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.event.OpenDetailWithEvent;
import com.heifeng.cloudshelf.mvp.model.DetailImageInfo;
import com.heifeng.cloudshelf.utils.DensityUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by admin on 2019/9/12.
 */

public class SearchHouseRvAdapter extends BaseQuickAdapter<DetailImageInfo, BaseViewHolder> {
    private Context context;

    public SearchHouseRvAdapter(Context context, int layoutResId, @Nullable List<DetailImageInfo> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, DetailImageInfo item) {

        LinearLayout.LayoutParams layoutParamsTop = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsTop.height = MyApp.SCREEN_HEIGHT / 8 + DensityUtils.dp2px(context, 25);
        layoutParamsTop.width = DensityUtils.dp2px(mContext,160);
        layoutParamsTop.setMargins(0, DensityUtils.dp2px(context, 18), DensityUtils.dp2px(context, 20), 0);

        LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsTop.height = MyApp.SCREEN_HEIGHT / 8 + DensityUtils.dp2px(context, 25);
        layoutParamsBottom.width = DensityUtils.dp2px(mContext,160);
        layoutParamsBottom.setMargins(0, DensityUtils.dp2px(context, 18), DensityUtils.dp2px(context, 20), 0);

        helper.getView(R.id.l_top).setLayoutParams(layoutParamsTop);
        helper.getView(R.id.l_bottom).setLayoutParams(layoutParamsBottom);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.height = DensityUtils.dp2px(mContext,160);
        helper.getView(R.id.iv_top).setLayoutParams(layoutParams);
        helper.getView(R.id.iv_bottom).setLayoutParams(layoutParams);


        RoundedCorners roundedCorners = new RoundedCorners(2);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
//        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//        options.skipMemoryCache(true);

        if (null != item.getFirstImage() && !TextUtils.isEmpty(item.getFirstImage().getImage())) {

            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(mContext).load(item.getFirstImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_top));

            helper.getView(R.id.l_top).setVisibility(View.VISIBLE);
            helper.getView(R.id.tv_top_title).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_top_title,item.getFirstImage().getName());
        } else {
            helper.getView(R.id.tv_top_title).setVisibility(View.INVISIBLE);
            helper.getView(R.id.l_top).setVisibility(View.INVISIBLE);
        }

        if (null != item.getSecondImage() && !TextUtils.isEmpty(item.getSecondImage().getImage())) {

            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(mContext).load(item.getFirstImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_bottom));

            helper.getView(R.id.l_bottom).setVisibility(View.VISIBLE);
            helper.getView(R.id.tv_bottom_title).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_bottom_title,item.getSecondImage().getName());
        } else {
            helper.getView(R.id.l_bottom).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_bottom_title).setVisibility(View.INVISIBLE);
        }

        helper.getView(R.id.iv_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDetail(item.getFirstImage().getObj() + "",item.getFirstImage().getName());
            }
        });

        helper.getView(R.id.iv_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDetail(item.getSecondImage().getObj() + "",item.getSecondImage().getName());
            }
        });
    }

    public void openDetail(String id,String title) {
        EventBus.getDefault().post(new OpenDetailWithEvent(id,title));
    }
}
